package com.gome.security.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.DigestInputStream;
import java.security.MessageDigest;

import org.apache.commons.codec.binary.Hex;

public class MD5Test {
	
	
	public static void main(String[] args) throws Exception {
		
	//文件路径
		String path="D:\\example-dump.txt";
		//构建文件输入流
		FileInputStream fis = new FileInputStream(new File(path));
		//初始化MessageDigest,并制定MD5算法
        DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5"));		
		//流缓冲大小
		int buf = 1024;
		//缓冲字节数组
		byte[] buffer = new byte[buf];
		//当读到值大于-1就继续读
		int read = dis.read(buffer,0,buf);
		
		while(read > -1){
			read = dis.read(buffer,0,buf);
		}
		
		dis.close();
		//获得MessageDigest
		MessageDigest md = dis.getMessageDigest();
		//摘要处理
		byte[] b = md.digest();
		//十六进制转换
		String md5hex = Hex.encodeHexString(b);
		//验证
		
		System.out.println(md5hex);
		
		
		
		
		
		
		
		
		
	}
	
	
	
	
	

}
